package cn.com.blueInfo.bpm.system.service.impl;

import cn.com.blueInfo.bpm.system.convert.ResourceConvert;
import cn.com.blueInfo.bpm.system.entity.Resource;
import cn.com.blueInfo.bpm.system.repository.ResourceRepository;
import cn.com.blueInfo.bpm.system.request.ResourceDTO;
import cn.com.blueInfo.bpm.system.response.ResourceVO;
import cn.com.blueInfo.bpm.system.service.ResourceService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资源表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceRepository resourceRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private ResourceConvert resourceConvert;

    /**
     * 新增资源
     * @param resourceDTO 资源参数
     */
    @Override
    public ResultInfo<Integer> addResource(ResourceDTO resourceDTO) {
        Resource resource = resourceConvert.dtoToEntity(resourceDTO);
        resource.setId(snowflakeIdGenerator.nextId());
        Integer addSize = resourceRepository.createResource(resource);
        return ResultInfo.<Integer>builder().data(addSize).message("资源成功新增 {} 条").build();
    }

    /**
     * 批量新增资源
     * @param resourceDTOList 资源参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddResource(List<ResourceDTO> resourceDTOList) {
        List<Resource> resourceList = resourceConvert.dtoToEntity(resourceDTOList);
        for (Resource resource : resourceList) resource.setId(snowflakeIdGenerator.nextId());
        Integer addSize = resourceRepository.batchCreateResource(resourceList);
        return ResultInfo.<Integer>builder().data(addSize).message("资源成功新增 {} 条").build();
    }

    /**
     * 删除资源
     * @param resourceId 资源ID
     */
    @Override
    public ResultInfo<Integer> removeResource(Long resourceId) {
        Integer removeSize = resourceRepository.deleteResource(resourceId);
        return ResultInfo.<Integer>builder().data(removeSize).message("资源成功删除 {} 条").build();
    }

    /**
     * 批量删除资源
     * @param resourceIdList 资源ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveResource(List<Long> resourceIdList) {
        Integer removeSize = resourceRepository.batchDeleteResource(resourceIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("资源成功删除 {} 条").build();
    }

    /**
     * 更新部分资源
     * @param resourceUpdatePartialDTO 资源参数
     */
    @Override
    public ResultInfo<Integer> modifyResourcePartial(ResourceDTO.UpdatePartialDTO resourceUpdatePartialDTO) {
        Resource resource = resourceConvert.dtoToEntity(resourceUpdatePartialDTO);
        Integer modifySize = resourceRepository.updateResourcePartial(resource);
        return ResultInfo.<Integer>builder().data(modifySize).message("资源成功更新 {} 条").build();
    }

    /**
     * 更新全部资源
     * @param resourceDTO 资源参数
     */
    @Override
    public ResultInfo<Integer> modifyResource(ResourceDTO resourceDTO) {
        Resource resource = resourceConvert.dtoToEntity(resourceDTO);
        Integer modifySize = resourceRepository.updateResource(resource);
        return ResultInfo.<Integer>builder().data(modifySize).message("资源成功更新 {} 条").build();
    }

    /**
     * 查询一条资源
     * @param resourceId 资源ID
     */
    @Override
    public ResultInfo<ResourceVO> queryResourceById(Long resourceId) {
        Resource resource = resourceRepository.getResourceById(resourceId);
        ResourceVO resourceVO = resourceConvert.entityToVO(resource);
        return ResultInfo.<ResourceVO>builder().data(resourceVO).message("资源查询成功").build();
    }

    /**
     * 分页查询资源
     * @param resourceQueryDTO 资源参数
     */
    @Override
    public ResultInfo<Page<ResourceVO>> queryResourcePage(ResourceDTO.QueryDTO resourceQueryDTO) {
        Page<Resource> resourcePage = resourceRepository.getResourcePage(resourceQueryDTO);
        Page<ResourceVO> resultData = resourceConvert.convertPage(resourcePage);
        return ResultInfo.<Page<ResourceVO>>builder()
                .data(resultData)
                .message("资源分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 资源 数量
     * @param resourceQueryDTO 资源参数
     */
    @Override
    public ResultInfo<Long> queryResourceCount(ResourceDTO.QueryDTO resourceQueryDTO) {
        Long resultCount = resourceRepository.getResourceCount(resourceQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("资源数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 资源
     * @param resourceQueryDTO 资源参数
     */
    @Override
    public ResultInfo<List<ResourceVO>> queryResourceList(ResourceDTO.QueryDTO resourceQueryDTO) {
        List<Resource> resourceList = resourceRepository.getResourceList(resourceQueryDTO);
        List<ResourceVO> resultData = resourceConvert.entityListToVoList(resourceList);
        return ResultInfo.<List<ResourceVO>>builder()
                .data(resultData)
                .message("资源分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
